<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/6
 * Time: 11:08
 */

namespace app\admin\model;


class Prop extends  DataBase
{

    public function indexData()
    {
        $where = [];
        $key = input('key');
        $value = input('value');
        if(!empty($key) && !empty($value)){
            if($key == 'status'){
                switch(trim($value)){
                    case '上架': $where[] = ['status','=','0'];break;
                    case '下架': $where[] = ['status','=','1'];break;
                    default : $where[] = [];
                }
            }else
                $where[] = [$key,'=',$value];
        }
        $order = input('sort','id').' '.input('order','asc');
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        );
        return $this->where($where)->order($order)->paginate($page_config);
    }
    //道具上下架
    public function changeStatus()
    {
        $id = input('id');
        $status = $this->where('id',$id)->value('status');
        $status = $status == 1 ? 2 : 1;
        $res =  $this->save(['status'=>$status],['id'=>$id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];

    }

    //道具添加
    public function PropAdd()
    {
        $newData['prop_name'] = input('post.prop_name');
        $newData['prop_pic'] = oosUpload($_FILES['prop_pic']);
        $newData['thumb_pic'] = oosUpload($_FILES['thumb_pic']);
        $newData['status'] = input('post.status');
        $newData['dec'] = input('post.dec');
        $newData['create_time'] = time();
        if(empty($newData['prop_name']) || empty($newData['prop_pic']) || empty($newData['status']))
            return ['code'=>-100,'msg'=>'数据不完整'];
        $res = $this->save($newData);
        if($res)   return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>100,'msg'=>'添加失败'];

    }
}